package com.jy.adscm.operation.aftersales.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.aftersales.bean.*;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShRepairUpkeepMaterialListCheckMapper;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShRepairUpkeepMaterialListMapper;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShylMaterialdetailMapper;
import com.jy.adscm.operation.aftersales.service.ShRepairUpkeepMaterialListService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.aftersales.service.ShylMaterialService;
import com.jy.adscm.operation.cg.bean.CgMaterial;
import com.jy.adscm.operation.cg.mapper.mysql.CgMaterialMapper;
import com.jy.adscm.operation.mscyb.bean.JqylSettingMaterialCheck;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqylSettingMaterialCheckMapper;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-08-29
 */
@Service
public class ShRepairUpkeepMaterialListServiceImpl extends ServiceImpl<ShRepairUpkeepMaterialListMapper, ShRepairUpkeepMaterialList> implements ShRepairUpkeepMaterialListService {
    @Autowired
    CgMaterialMapper cgMaterialMapper;
    @Autowired
    ShylMaterialService shylMaterialService;
    @Autowired
    ShylMaterialdetailMapper shylMaterialdetailMapper;
    @Autowired
    ShRepairUpkeepMaterialListCheckMapper shRepairUpkeepMaterialListCheckMapper;
    @Autowired
    JqylSettingMaterialCheckMapper jqylSettingMaterialCheckMapper;
    @Autowired
    JyUserMapper jyUserMapper;
    @Override
    public boolean insertShRepairUpkeepMaterialList(ShRepairUpkeepMaterialList shRepairUpkeepMaterialList) throws AdscmSystemException {
        //查询该物料是否有编码
        CgMaterial cgMaterial = cgMaterialMapper.selectById(shRepairUpkeepMaterialList.getName());
        if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getCode(), null)) {
            String code = "s" + cgMaterial.getCode() + new SimpleDateFormat("yyMMdd").format(new Date());
            //查询是否有更大的编码
            Wrapper<ShRepairUpkeepMaterialList> shRepairMaterialListWrapper = new EntityWrapper<>();
            shRepairMaterialListWrapper.like("code", code, SqlLike.RIGHT);
            shRepairMaterialListWrapper.last("limit 1");
            List<ShRepairUpkeepMaterialList> shRepairMaterialLists = super.selectList(shRepairMaterialListWrapper);
            if (shRepairMaterialLists != null && shRepairMaterialLists.size() > 0) {
                ShRepairUpkeepMaterialList shRepairMaterialList1 = shRepairMaterialLists.get(0);
                code = code + String.valueOf(Integer.valueOf(shRepairMaterialList1.getCode().replace(code, "")) + 1);
            } else {
                code = code + "01";
            }

            shRepairUpkeepMaterialList.setCode(code);
        }

        boolean insert = super.insert(shRepairUpkeepMaterialList);
        //减库存
        ShylMaterial shylMaterial = shylMaterialService.updateShylMaterialValue(shRepairUpkeepMaterialList.getKid().toString(), -shRepairUpkeepMaterialList.getNum());
        //添加出库记录
        ShylMaterialdetail shylMaterialdetail = new ShylMaterialdetail();
        shylMaterialdetail.setOutstoragetime(new Date());
        shylMaterialdetail.setTid(shylMaterial.getTid().toString());
        shylMaterialdetail.setName(shylMaterial.getResourcename());
        shylMaterialdetail.setUseperson(shRepairUpkeepMaterialList.getCreator());
        shylMaterialdetail.setUsenums(shRepairUpkeepMaterialList.getNum().doubleValue());
        shylMaterialdetail.setMaterialstate("1");
        shylMaterialdetail.setBatchnums(shylMaterial.getBatchnums());
        shylMaterialdetail.setPid(shRepairUpkeepMaterialList.getPid().toString());
        shylMaterialdetailMapper.insert(shylMaterialdetail);
        //查询如果有检验记录生成对应的检验记录
        Wrapper<JqylSettingMaterialCheck> jqylSettingMaterialCheckWrapper = new EntityWrapper<>();
        jqylSettingMaterialCheckWrapper.eq("pid", shRepairUpkeepMaterialList.getName());
        List<JqylSettingMaterialCheck> jqylSettingMaterialChecks = jqylSettingMaterialCheckMapper.selectList(jqylSettingMaterialCheckWrapper);
        if (jqylSettingMaterialChecks != null && jqylSettingMaterialChecks.size() > 0) {
            for (JqylSettingMaterialCheck jqylSettingMaterialCheck : jqylSettingMaterialChecks) {
                ShRepairUpkeepMaterialListCheck shRepairMaterialListCheck = new ShRepairUpkeepMaterialListCheck();
                shRepairMaterialListCheck.setPid(shRepairUpkeepMaterialList.getId());
                shRepairMaterialListCheck.setCid(jqylSettingMaterialCheck.getId());
                shRepairMaterialListCheck.setName(jqylSettingMaterialCheck.getName());
                shRepairMaterialListCheck.setType(jqylSettingMaterialCheck.getType());
                shRepairMaterialListCheck.setUnit(jqylSettingMaterialCheck.getUnit());
                shRepairMaterialListCheck.setRemark(jqylSettingMaterialCheck.getRemark());
                shRepairMaterialListCheck.setReference(jqylSettingMaterialCheck.getReference());
                shRepairMaterialListCheck.setRank(jqylSettingMaterialCheck.getRank());
                shRepairUpkeepMaterialListCheckMapper.insert(shRepairMaterialListCheck);
            }
        } else {
            //免检物料
            shRepairUpkeepMaterialList.setState("1");
            shRepairUpkeepMaterialList.setIsCheck(1);
        }
        return insert;
    }

    @Override
    public boolean deleteShRepairUpkeepMaterialList(Integer id, Token token) throws AdscmSystemException{
        ShRepairUpkeepMaterialList shRepairUpkeepMaterialList = super.selectById(id);
        //恢复库存
        ShylMaterial shylMaterial = shylMaterialService.updateShylMaterialValue(shRepairUpkeepMaterialList.getKid().toString(), shRepairUpkeepMaterialList.getNum());
        //删除出库记录
        Wrapper<ShylMaterialdetail> wrapper=new EntityWrapper<>();
        wrapper.eq("tid",shylMaterial.getTid());
        wrapper.eq("pid",shRepairUpkeepMaterialList.getPid());
        shylMaterialdetailMapper.delete(wrapper);
        Wrapper<ShRepairUpkeepMaterialListCheck> shRepairMaterialListCheckWrapper=new EntityWrapper<>();
        shRepairMaterialListCheckWrapper.eq("pid",id);
        List<ShRepairUpkeepMaterialListCheck> shRepairUpkeepMaterialListChecks = shRepairUpkeepMaterialListCheckMapper.selectList(shRepairMaterialListCheckWrapper);
        if (shRepairUpkeepMaterialListChecks!=null&& shRepairUpkeepMaterialListChecks.size()>0){
            for (ShRepairUpkeepMaterialListCheck shRepairUpkeepMaterialListCheck : shRepairUpkeepMaterialListChecks) {
                shRepairUpkeepMaterialListCheckMapper.deleteById(shRepairUpkeepMaterialListCheck.getId());
            }
        }
        boolean b = super.deleteById(id);
        return b;
    }

    @Override
    public List<ShRepairUpkeepMaterialList> selectBypid(Integer pid) {
        Wrapper<ShRepairUpkeepMaterialList> wrapper=new EntityWrapper<>();
        wrapper.eq("pid",pid);
        List<ShRepairUpkeepMaterialList> shRepairUpkeepMaterialLists = super.selectList(wrapper);
        for (ShRepairUpkeepMaterialList shRepairMaterialList : shRepairUpkeepMaterialLists) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(shRepairMaterialList.getName(),null)){
                CgMaterial cgMaterial = cgMaterialMapper.selectById(shRepairMaterialList.getName());
                shRepairMaterialList.setNameName(cgMaterial.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(shRepairMaterialList.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(shRepairMaterialList.getCreator());
                shRepairMaterialList.setCreatorName(jyUser.getName());
            }
        }
        return shRepairUpkeepMaterialLists;
    }
}
